package com.jqsoft.launcher.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;

import com.jqsoft.launcher.activity.JQActivity;

import java.lang.reflect.Method;

/**
 * android设置信息类
 * 
 * @author gujunqi date：2013-12-24
 */
public class AndroidUtil {

	// 设备的分辨率
	private static int[] distinguishability;
	private static String tag = "AndroidUtil";

	/*
	 * 获取手机序列号
	 */
	public static String getDeviceId(Context ctx) {
		String deviceID = "";
		TelephonyManager tm = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getDeviceId() != null) {
			deviceID = tm.getDeviceId();
			Log.d("UTIL", "deviceID:" + deviceID);
		}
		return deviceID;
	}

	// 获取手机的设备信息
	public static String getClientDeviceInfo(Context ctx) {
		String deviceID = "";
		String serial = "";
		TelephonyManager tm = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getDeviceId() != null) {
			deviceID = tm.getDeviceId();
			try {
				Class<?> c = Class.forName("android.os.SystemProperties");
				Method get = c.getMethod("get", String.class);
				serial = (String) get.invoke(c, "ro.serialno");
			} catch (Exception e) {
				Log.e(tag, "get the system sn ERROR!", e);
			}
			Log.d("serial", "deviceID:" + deviceID);
		}
		String buildVersion = android.os.Build.VERSION.RELEASE;
		return deviceID + "|" + serial + "|android " + buildVersion;
	}

	// 获取设备的品牌等信息
	public static String getDeviceBranch(Context ctx) {
		String mobileBrand = android.os.Build.BRAND + "-"
				+ android.os.Build.MODEL;
		String deviceID = "";
		TelephonyManager tm = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getDeviceId() != null) {
			deviceID = tm.getDeviceId();
		}
		String buildVersion = android.os.Build.VERSION.RELEASE;
		return deviceID + "|" + mobileBrand + "|android " + buildVersion;
	}

	// 获取设备的品牌等信息
	public static String getBranch(Context ctx) {
		String mobileBrand = android.os.Build.BRAND + "-"
				+ android.os.Build.MODEL;
		return mobileBrand;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 取得操作系统版本号
	 */
	public static String getOSVersion() {
		return android.os.Build.VERSION.RELEASE;
	}

	/**
	 * 取得应用的版本号
	 */
	public static String getAPKVersion(Context ctx) {
		PackageManager packageManager = ctx.getPackageManager();
		try {
			PackageInfo packageInfo = packageManager.getPackageInfo(
					ctx.getPackageName(), 0);
			return packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 验证操作系统版的本号是否小于2.0
	 *
	 * @return true version< 2.0,false >=2.0
	 */
	public static boolean oSVersionIsLow2Version(Context ctx) {
		int version = Integer.valueOf(android.os.Build.VERSION.SDK);
		Log.i(tag, "version:" + version);
		// if (version >= 5) { // 2.0

		return version >= 5 ? false : true;
	}

	/**
	 * 取得设备的分辨率,获得手机的宽度和高度像素单位为px
	 *
	 * @return r[0]:宽 ,r[1]:长
	 */
	public static int[] getDeviceDistinguishability(Activity ctx) {
		if (null == distinguishability) {
			DisplayMetrics dm = new DisplayMetrics();
			ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
			distinguishability = new int[] { dm.widthPixels, dm.heightPixels };
		}
		Log.d("AndroidUtil", "手机的宽度和高度:" + distinguishability[0] + "*"
				+ distinguishability[1]);
		return distinguishability;
	}

	/**
	 * 获取手机WiFi MAC 地址
	 */
	public static String getMACAddress(Context ctx) {
		WifiManager wifi = (WifiManager) ctx
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		String macAddress = info.getMacAddress();// 4C:AA:16:24:F5:43
		Log.d(tag, "macAddress:" + macAddress);
		if (null == macAddress)
			return "";
		return macAddress;
	}

	/** 发送短信 */
	public static void sendMsg(JQActivity act, String msg, String number) {
		Log.i(tag, "发送的msg:" + msg + "   手机号码:" + number);
		Uri uri = Uri.parse("smsto:" + number);
		Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
		intent.putExtra("sms_body", msg);
		act.startActivity(intent);
	}

	/** 隐藏键盘 */
	public static void hideKeyBord(JQActivity act) {
		// 隐藏键盘
		InputMethodManager imm = (InputMethodManager) act
				.getSystemService(act.INPUT_METHOD_SERVICE);
		if (act.getCurrentFocus() != null) {
			imm.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		}

	}

	 
}